package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysProduct;
import com.mars.module.admin.entity.SysShopCar;
import com.mars.module.system.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserAddressRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserAddressMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserAddress;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserAddressService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户收货地址业务层处理
 *
 * @author mars
 * @date 2024-05-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserAddressServiceImpl implements ISysUserAddressService {

    private final SysUserAddressMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserAddress add(SysUserAddressRequest request) {
        SysUserAddress entity = SysUserAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long id = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(id);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserAddressRequest request) {
        SysUserAddress entity = SysUserAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserAddress getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserAddress> pageList(SysUserAddressRequest request) {
        Page<SysUserAddress> page = new Page<>(request.getPageNo(), request.getPageSize());
        request.setUserId(ContextUserInfoThreadHolder.get().getId());
        LambdaQueryWrapper<SysUserAddress> query = this.buildWrapper(request);
        IPage<SysUserAddress> pageRecord = baseMapper.selectPage(page, query);
        List<SysUserAddress> records = pageRecord.getRecords();
        List<SysUserAddress> recordsNew = new ArrayList<>();
        for (SysUserAddress address : records) {
            address.setAddressShow(address.getName() + "-" + address.getAddress() + "-" + address.getTelephone());
            recordsNew.add(address);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserAddress> list(SysUserAddressRequest request) {
        LambdaQueryWrapper<SysUserAddress> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysUserAddress> buildWrapper(SysUserAddressRequest param) {
        LambdaQueryWrapper<SysUserAddress> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getTelephone())) {
            query.like(SysUserAddress::getTelephone, param.getTelephone());
        }
        if (StringUtils.isNotBlank(param.getAddress())) {
            query.like(SysUserAddress::getAddress, param.getAddress());
        }
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysUserAddress::getName, param.getName());
        }
        if (param.getUserId() != null) {
            query.eq(SysUserAddress::getUserId, param.getUserId());
        }
        return query;
    }

}
